<?php

class NewsAction extends AdministratorAction {
	
	protected $modelObj = null;
	
	public function _initialize(){
		$this->modelObj = D('News');
		
	}
	
	public function index(){
		import('ORG.Util.Page');
		//每页显示数
		$perpage = C('PER_PAGE');
		$keyword = $this->_get('keyword');
		$where = array();
		if ( $keyword !='' ){
			$where[] = "title LIKE '%$keyword%'";
			$where[] = "content LIKE '%$keyword%'";
		}
		$where_str = implode(' OR ', $where);
		$count = $this->modelObj->where($where_str)->count();
		$Page = new Page($count,$perpage);
		$show = $Page->show();
		$news = $this->modelObj->where($where_str)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('news',$news);
		$this->assign('page',$show);
		$this->display();
	}
	
	
	/**
	 * 发布文章
	 */
	public function publish(){
		if ( $this->isPost() ){
			$id = $_POST['nid'];
		}
		else{
			$id = $this->_get('nid');
		}
		$where = array();
		if ( is_array($id) ){
			$idstr = implode(',', $id);
			$where['id'] = array('IN',$idstr);
		}
		else{
			$where['id'] = array('EQ',$id);
		}
		if ( $this->modelObj->where($where)->setField('public',1) ){
			$this->success('操作成功');
		}
		else{
			$this->success('操作失败');
		}
	}
	
	/**
	 * 停止发布文章
	 */
	public function stopPublish(){
		if ( $this->isPost() ){
			$id = $_POST['nid'];
		}
		else{
			$id = $this->_get('nid');
		}
		$where = array();
		if ( is_array($id) ){
			$idstr = implode(',', $id);
			$where['id'] = array('IN',$idstr);
		}
		else{
			$where['id'] = array('EQ',$id);
		}
		if ( $this->modelObj->where($where)->setField('public',0) ){
			$this->success('操作成功');
		}
		else{
			$this->success('操作失败');
		}
	}
	
	protected function _checkFormData(){
	
	}
}